function i_cmp(r1, nr, skip, spikethresh, varargin)
%
% function to be called from the datac command line.
% Will make a new figure if not already existing called
% i_cmp.
% the current (I) data will be gotten from datac
% and the records r1:skip:(r1+nr) averaged and plotted
% subsequent calls add to current display.
% A call with no parameters clears the current display.
% a call with more parameters displays the data in multiple subplots
% with the same parameters as the first display.
% 8/28/2000 P. Manis.
% For current: 11/27/2000
%
h = findobj('Tag', 'i_cmp'); % look for the figure
if(isempty(h))
   h = figure;
   set(h, 'Tag', 'i_cmp');
   set(h, 'Name', 'I Compare');
end;
figure(h);
if(nargin == 0)
   clf;
   set(h, 'UserData', []);
   return;
end;

v = datac('geti');
d = datac('getdfile');
c = datac('sel');
rl = d.record;
np=1+length(varargin);
allrs = [r1 varargin{:}]

if(length(varargin) > 0 & varargin{end} == '+') % last argument is + for append
   allrs = [r1 varargin{1:end-1}];
else
   clf;
   h = findobj('Tag', 'i_cmp'); % look for the figure
   set(h, 'UserData', []);
end;

nc = get(h,'UserData');
if(isempty(nc)) nc = 0; end;
tb = make_time(d);
tb = tb(1,:);

color=['k', 'r', 'b', 'g', 'c', 'y', 'k'];
nrec = [];
np=0;
RATES = d.rate*d.nr_channel/1000;
va = zeros(1,size(v,2));
for rs = allrs
   [rln, k] = intersect(rl, [rs:skip:(rs+nr)]);
   rlx = rln - rl(1) + 1;
 %  rlx
   if(isempty(rln))
      fprintf(2, 'Records %d-%d not found in current dataset\n', r1, r1+nr);
      return;
   end;
   np=np+1;
   for i=1:length(rlx)
      fsamp = 1000/RATES(i); % get sampling frequency
      fco = 300;		% cutoff frequency in Hz
      wco = fco/(fsamp/2); % wco of 1 is for half of the sample rate, so set it like this...
      fprintf('wco: %8.5f\n', wco);
      if(wco < 1) % if wco is > 1 then this is not a filter!
         disp('filt')
         [b, a] = fir1(24, wco); % fir type filter... seems to work best, with highest order min distortion of dv/dt...
         vs(rlx(i),:) = filtfilt(b, a, v(rlx(i),:)); % filter all the traces...
      else
         vs(rlx(i),:) = v(rlx(i),:);
         disp('no filt')
         end;
         tbe = floor(5./RATES(i)) + 1;
         b = mean(vs(rlx(i),1:tbe));
         vs(rlx(i),:) = vs(rlx(i),:) - b;
      end
   va(np,:)=mean(vs(rlx,:));
%   size(va)
   nc = nc + 1;
end;
%tpat = [tb tb(end:-1:1)];
%[s, u] = sort(vstd); % sort standard deviations in reverse order
%u = fliplr(u); % reverse
colormap(colorcube);
v=colormap;
subplot('position', [0.1 0.4 0.8 0.55]);
hold off;
for i = 1:np
   plot(tb, va(i,:), 'color', v(i,:));
   hold on;
end;
subplot('position', [0.1 0.07 0.8 0.25]);
if(np == 2)
   plot(tb, va(2,:)-va(1,:), 'color', v(1,:));
end;


return;


function [w] = lwx(nc, i)
w = 0.333*(rem(nc, i)+1);
return;

